﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Dynamic;
using System.Data.SqlClient;
using System.Configuration;
using Model;
using Common;
namespace DAL
{
    public class ConfigDAL
    {

        public static ConfigInfo FindOneByID(int id)
        {
            DataRow dr = CommonDAL.FindOneByID(id, "[config]");
            return Simple_Data(dr);
        }
        public static int Update(ConfigInfo info)
        {
            string sql = string.Format("update [config] set [Token]=@Token,[Ticket]=@Ticket,[LastEditTime]=@LastEditTime where [ID]=@ID");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@Token", info.Token),
                new SqlParameter("@Ticket", info.Ticket),
                new SqlParameter("@LastEditTime", info.LastEditTime),
                new SqlParameter("@ID", info.ID)
            };
            return CommonDAL.ExecuteNoneQuery(sql, paras) ? info.ID : 0;
        }
        public static int Edit(ConfigInfo info)
        {
            string sql = string.Format("update [config] set [Logo]=@Logo,[WebName]=@WebName,[Title]=@Title,[Keyword]=@Keyword,[Description]=@Description where [AdminID]=@AdminID");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@Logo", info.Logo),
                new SqlParameter("@WebName", info.WebName),
                new SqlParameter("@Title", info.Title),
                new SqlParameter("@Keyword", info.Keyword),
                new SqlParameter("@Description", info.Description),
                new SqlParameter("@AdminID", info.AdminID)
            };
            return CommonDAL.ExecuteNoneQuery(sql, paras) ? info.ID : 0;
        }
        private static ConfigInfo Simple_Data(DataRow dr)
        {
            ConfigInfo info = new ConfigInfo();
            if (dr != null)
            {
                info.ID = DataConvert.ToInt(dr["ID"]);
                info.AdminID = DataConvert.ToInt(dr["AdminID"]);
                info.Logo = DataConvert.ToString(dr["Logo"]);
                info.WebName = DataConvert.ToString(dr["WebName"]);
                info.Title = DataConvert.ToString(dr["Title"]);
                info.Keyword = DataConvert.ToString(dr["Keyword"]);
                info.Description = DataConvert.ToString(dr["Description"]);
            }
            return info;
        }
        public static ConfigInfo GetConfig()
        {
            WeiXin wx = new WeiXin();
            ConfigInfo config = FindOneByID(1);
            if (DateTime.Now > config.LastEditTime.AddHours(1))
            {
                string token = wx.HttpGet("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx1696443fbd5c7d06&secret=0fcd69e251da5f23aba1c090f97ed79d", "");
                token = wx.ConvertJson(token).access_token;
                string ticket = GetTickect(token);
                ticket = wx.ConvertJson(ticket).ticket;
                config.Token = token;
                config.Ticket = ticket;
                Update(config);
            }
            return config;
        }

        public static string GetTickect(string access_token)
        {
            WeiXin wx = new WeiXin();
            var url = string.Format("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={0}&type=jsapi", access_token);
            return wx.HttpGet(url, "");
        }
    }
}
